package com.hope.aimouse;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;


public class StartActivity extends Activity {
        private AudioRecord audioRecord;
        private boolean isRecording = false;

//        private void getPermission(){
//
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                if (checkSelfPermission(Manifest.permission.RECORD_AUDIO)
//                        != PackageManager.PERMISSION_GRANTED) {
//                    requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 1);
//                }
//            }
//        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // ... 其他初始化代码 ...

//            getPermission();

            // 设置键盘监听
            getWindow().getDecorView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // 检测是否是 F10 键按下事件
                    if (keyCode == KeyEvent.KEYCODE_F10 && event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (!isRecording) {
                            Log.d(Constant.TAG, "f10 keydown");
                            startAudioRecording();
                        } else {
                            stopAudioRecording();
                        }
                        return true;
                    }

                    switch (keyCode){
                        case KeyEvent.KEYCODE_F10:
                            CommonUtils.takeScreenshot(getBaseContext());
                            break;
                        case KeyEvent.KEYCODE_F12:
                            CommonUtils.launchApp(Constant.AiApp, getBaseContext());
                            break;
                        case KeyEvent.KEYCODE_F9:
                            CommonUtils.launchApp(Constant.AudioApp, getBaseContext());
                            break;
                        default:
                            break;
                    }
                    return false;
                }
            });
        }

        private void startAudioRecording() {
            // 设置音频参数
            int sampleRate = 44100;
            int channelConfig = AudioFormat.CHANNEL_IN_MONO;
            int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
            int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);

            try {
                // 创建 AudioRecord 实例
                audioRecord = new AudioRecord(
                        MediaRecorder.AudioSource.MIC,
                        sampleRate,
                        channelConfig,
                        audioFormat,
                        bufferSize
                );

                // 开始录音
                audioRecord.startRecording();
                isRecording = true;

                // 在新线程中处理音频数据
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        byte[] buffer = new byte[bufferSize];
                        while (isRecording) {
                            int readSize = audioRecord.read(buffer, 0, bufferSize);
                            if (readSize > 0) {
                                // 在这里处理录制的音频数据
                                processAudioData(buffer, readSize);
                            }
                        }
                    }
                }).start();

            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }

        private void stopAudioRecording() {
            if (audioRecord != null) {
                isRecording = false;
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;
            }
        }

        private void processAudioData(byte[] buffer, int size) {
            // 在这里添加音频数据处理逻辑
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            stopAudioRecording();
        }
}
